import { defineStore } from "pinia";

export const useSearchStore = defineStore('search', {
  state: () => ({
    historys: []
  }),
  actions: {
    /**
     * 添加搜索历史
     * @param {*} search
     */
    addHistory(search) {
      const isFindIndex = this.historys.findIndex(item => item === search);
      if (isFindIndex !== -1) {
        this.historys.splice(isFindIndex, 1);
      }
      this.historys.unshift(search);
    },
    /**
     * 删除搜索历史
     * @param {*} index
     */
    delHistory(index) {
      this.historys.splice(index, 1);
    },
    /**
     * 清空搜索历史
     */
    clearHistory() {
      this.historys = [];
    }
  },
  persist: true
})